﻿import flash.geom.Point;
import flash.geom.Matrix;
import flash.geom.ColorTransform;
import flash.geom.Transform;
import flash.display.BitmapData;
import flash.filters.DropShadowFilter;

class lib.modula.HintWindow extends MovieClip{
	
	//public paramaters
	public var container:MovieClip;
	public var text:String;
	public var htmlText:String;
	public var textSize:Number=12;
	public var _margin:Number=2;
	public var _border:Number=1;
	public var borderColor:Number=0x555555;
	public var gbgColor1:Number=0xf3f3f3;
	public var gbgColor2:Number=0xAAAAAA;
	public var innerRadius:Number=3;
	public var drapShadowColor:Number=0x000000;
	public var drapShadowAlpha:Number=65;
	public var drapShadowDist:Number = 5;
	public  var _xOffset:Number=15;
	public  var _yOffset:Number=15;
	
	//
	private var globlePos:Point;
	private var isHtml:Boolean=false;
	private var targetMC:MovieClip;
	private var startPos:Point;
	private var rightSide:Boolean;
	private var topSide:Boolean;
	private var _tf:TextField;
	private var lowestDepth:Number;
	private var bg:MovieClip;
	private var dispDelay:Number=3;
	private var fadeInInterval:Number;
	private var fadeOutInterval:Number;
	private var fadeColorTransform:ColorTransform;
	private var fadeTransform:Transform;
	private var bitmapLayer:MovieClip;
	
	
	function HintWindow(target:MovieClip,containers:MovieClip,contents:String,html:Boolean,delay:Number){
		if(target==null){
			return;
		}
		targetMC = target;
		container = _root;
		if(containers != _root){
			container = containers;
		}
		if(html){
			isHtml = html;
			htmlText = contents;
		}else{
			this.text=contents;
		}
		if(delay!=null){
			dispDelay = delay;
		}
		lowestDepth = container.getNextHighestDepth();
		draw();
	}
	public function draw(){
		//create text
		bg = container.createEmptyMovieClip("hintBg_mc",lowestDepth+1);
		_tf = container.createTextField("hint"+Math.floor(Math.random()*5000),lowestDepth+2,0,0,15,textSize+10);
		_tf.autoSize = "left";
		_tf.multiline = true;
		_tf.wordWrap = false;
		_tf.html = isHtml;
		_tf.selectable = false;
		if(isHtml){
			_tf.htmlText = htmlText;
		}else{
			_tf.text = text;
		}
		
		//
		var localPos:Point = new Point(0,0);
		targetMC.localToGlobal(localPos);
		startPos = new Point();
		
		if(localPos.x+targetMC._width+_tf._width+_xOffset>Stage.width){
			rightSide = false;
		}else{
			rightSide = true;
		}
		if(localPos.y+targetMC._height+_tf._width+_yOffset>Stage.height){
			topSide = true;
		}else{
			topSide = false;
		}
		//
		startPos = new Point(localPos.x,localPos.y);
		if(container == _root){
			
		}else{
			var conPos:Point = new Point(0,0);
			container.localToGlobal(conPos);
			startPos.x -=conPos.x;
			startPos.y -=conPos.y-targetMC._height/2+textSize/2;
		}
		if(rightSide){
			_tf._x = startPos.x+targetMC._width+_xOffset;
		}else{
			_tf._x = startPos.x-(_tf._width+_xOffset);
			
		}
		if(topSide){
			_tf._y = startPos.y-targetMC._height/2-_yOffset;
		}else{
			_tf._y = startPos.y+targetMC._height/2+_yOffset;
		}
		//
		
		padBg();
		//bitmap 
		bitmapLayer = container.createEmptyMovieClip("bitmapLayer",lowestDepth+3);
		bitmapLayer._x = _tf._x;
		bitmapLayer._y = _tf._y;
		var myBitmapData:BitmapData = new BitmapData(_tf._width, _tf._height, true, 0x000000);
		bitmapLayer.attachBitmap(myBitmapData,bg.getNextHighestDepth());
		myBitmapData.draw(_tf);
		_tf.removeTextField();
		bitmapLayer._visible =false;
		bg._visible = false;
		//_tf._alpha=0;
		/*fadeTransform = new Transform(bg);
		fadeColorTransform = new ColorTransform();
		fadeColorTransform.alphaMultiplier = 0;
		fadeTransform.colorTransform = fadeColorTransform;*/
		//trace(_tf._alpha);
	}
	//fill backgroud
	private function padBg(){
		
		bg.beginFill(borderColor,100);
		drawSlikeBox(bg,_tf._x-_margin-_border,_tf._y-_margin-_border,_tf._width+_margin*2+_border*2,_tf._height+_margin*2+_border*2,innerRadius+_border);
		bg.endFill();
		
		var fillType:String = "linear"
		var colors:Array = [gbgColor2, gbgColor1];
		var alphas:Array = [100, 100];
		var ratios:Array = [0x00, 0xFF];
		var matrix:Matrix = new Matrix();
		//GradientFill
		matrix.createGradientBox(_tf._height, _tf._width, 1.5*Math.PI, _tf._x-_margin*2, _tf._y-_margin*2);
		var spreadMethod:String = "pad";
		bg.beginGradientFill(fillType, colors, alphas, ratios, matrix, spreadMethod); 
		drawSlikeBox(bg,_tf._x-_margin,_tf._y-_margin,_tf._width+_margin*2,_tf._height+_margin*2,innerRadius);
		//attach filter
		bg.endFill();
		var distance:Number = drapShadowDist;
		var angleInDegrees:Number = 45;
		var color:Number = drapShadowColor;
		var alpha:Number = drapShadowAlpha/100;
		var blurX:Number = drapShadowDist;
		var blurY:Number = drapShadowDist;
		var strength:Number = 1;
		var quality:Number = 3;
		var inner:Boolean = false;
		var knockout:Boolean = false;
		var hideObject:Boolean = false;
		var filter:DropShadowFilter = new DropShadowFilter(distance, angleInDegrees, color, alpha, blurX, blurY, strength, quality, inner, knockout, hideObject);
		var filterArray:Array = new Array();
		filterArray.push(filter);
		bg.filters = filterArray;


	}
	public function fadeIn(dur:Number,sec:Number){
		clearInterval(fadeInInterval);
		bitmapLayer._visible =bg._visible = true;
		bitmapLayer._alpha = bg._alpha=0;
		fadeInInterval = setInterval(this,"timeOut",dur*1000,{func:"fadeIn",sec:sec});
	}
	public function fadeOut(dur:Number,sec:Number){
		clearInterval(fadeInInterval);
		bitmapLayer._visible =bg._visible = true;
		bitmapLayer._alpha = bg._alpha=100;
		fadeInInterval = setInterval(this,"timeOut",dur*1000,{func:"fadeOut",sec:sec});
	}
	public function hide(){
		clearInterval(fadeInInterval);
		
		bitmapLayer._visible=false;
		bg._visible=false;
	}
	public function show(){
		clearInterval(fadeInInterval);
		bitmapLayer._visible=true;
		bg._visible=true;
	}
	private function timeOut(param){
		if(param.func =="fadeIn"){
			new mx.transitions.Tween(bitmapLayer, "_alpha", mx.transitions.easing.Strong.easeIn, 0, 100, 1, true);
			new mx.transitions.Tween(bg, "_alpha", mx.transitions.easing.Strong.easeIn, 0, 100, 1, true);
		}
		if(param.func =="fadeOut"){
			new mx.transitions.Tween(bitmapLayer, "_alpha", mx.transitions.easing.Strong.easeOut, 100, 0, 1, true);
			new mx.transitions.Tween(bg, "_alpha", mx.transitions.easing.Strong.easeOut, 100, 0, 1, true);
		}
		clearInterval(fadeInInterval);
	}
	function drawSlikeBox(target:MovieClip,x:Number,y:Number,width:Number,height:Number,radius:Number){
		target.moveTo(x,y+radius);
		target.curveTo(x,y,x+radius,y);
		target.lineTo(x+width-radius,y);
		target.curveTo(x+width,y,x+width,y+radius);
		target.lineTo(x+width,y+height-radius);
		target.curveTo(x+width,y+height,x+width-radius,y+height);
		target.lineTo(x+radius,y+height);
		target.curveTo(x,y+height,x,y+height-radius);
		target.lineTo(x,y+radius);
	}
	public function invalidate(){
		draw();
	}
	public function removeMovieClip(){
		bitmapLayer.removeMovieClip();
		bg.removeMovieClip();
		clearInterval(fadeInInterval);
		delete globlePos;
		delete isHtml;
		delete targetMC;
		delete startPos;
		delete rightSide;
		delete topSide;
		delete _tf;
		delete lowestDepth;
		delete bg;
		delete dispDelay;
		delete fadeInInterval;
		delete fadeOutInterval;
		delete fadeColorTransform;
		delete fadeTransform;
		delete bitmapLayer;
	}
}
